<?php

namespace Apps\Manage\Controllers;

use Apps\Common\Enum\SearchDataType;
use Apps\Common\Libs\SearchASync;
use Apps\Common\Libs\UploadPic;
use Apps\Common\Models\BeautyParlorService;
use Phalcon\Mvc\View;
use PhalconPlus\Common\ResponseResult;
use PhalconPlus\Common\ResponseResultStatus;

/**
 * 美容院服务管理
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class BpserviceController extends BpBaseController
{
    public function initialize()
    {
        parent::initialize();
        $this->setMainNav('bp');
    }

    /**
     * 美容院服务列表
     */
    public function listAction()
    {
        $this->setLeftNav('service_list');
        $req = $this->request;
        $id = intval($req->getQuery('id', null, 0));
        $serviceName = $req->getQuery('servicename',null,'');
        $serviceState = intval($req->getQuery('serviceState',null,-2));
        if($id <= 0) {
            $this->view->disable();
            echo '美容院不存在！';
            return;
        }
        if($serviceName!=''){
        	$where[] = "service_name like '%".$serviceName."%'";
        	$this->view->setVar('servicename',$serviceName);
        }
        if(!in_array($serviceState,[-2,-1,0,1])){
        	$this->view->disable();
        	echo '参数错误！';
        	return;
        }
        if($serviceState > -2){
        	$where[] = "service_state = $serviceState";
        }
        $where[]="beauty_parlor_id = $id";
        $whereStr = implode(" and ",$where);
        $list = BeautyParlorService::query()->where($whereStr)->execute();
        $this->view->setVar('serviceList', $list);
        $this->view->setVar('id', $id);
        $this->view->setVar('service_state',$serviceState);
    }
    /**
     * 添加美容院服务项目
     */
    public function addAction()
    {
        $this->setLeftNav('service_add');
        $req = $this->request;
        $id = intval($req->getQuery('id', null, 0));
        if($id <= 0) {
            $this->view->disable();
            echo '美容院不存在！';
            return;
        }
        $this->view->setVar('id', $id);
        //$this->getBPInfo($id);

        if(!$req->isPost()) {
            return;
        }
        $response = new ResponseResult();
        $response->callback = $req->getPost('callback', null, 'parent.setFormResult');
        $response->callbackJavascriptTag = true;
        $id = intval($req->getPost('beauty_parlor_id', null, 0));
        if($id <= 0) {
            $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '参数问题！');
            return $response;
        }
        $name = $req->getPost('service_name', null, '');
        //$description = $req->getPost('service_description', null, '');|| empty($description)
        $price = $req->getPost('service_price', null, '');
        $unit = $req->getPost('service_unit', null, '');
        $isSell = intval($req->getPost('service_is_sell', null, 0));
        $intro = $req->getPost('service_intro', null, '');

        // 判断空数据
        if(empty($name) || empty($price) || empty($intro)) {
            $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '参数异常！');
            return $response;
        }

        // 保存封面
        $uploadFile = new UploadPic();
        $uploadFile->request = $req;
        $cover = $uploadFile->beautyParlorCover();
        if($cover == false || !is_array($cover) || count($cover) == 0 || $cover[0] == false) {
            $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '封面必须上传！');
            return $response;
        }
        $cover = $cover[0];
        $service = new BeautyParlorService();
        $service->beauty_parlor_id = $id;
        $service->service_name = $name;
        $service->service_cover = $cover;
        $service->service_description = $description;
        $service->service_intro = $intro;
        $service->service_price = $price;
        $service->service_unit = $unit;
        $service->service_is_sell = $isSell;
        if($service->save() == false) {
            $this->databaseErrorLog($service);
            $response->sendError(ResponseResultStatus::DATABASE_ERROR, '保存数据异常！');
        } else {
            $response->sendResult($service->service_id);
            SearchASync::Instance()->noticeSync($service->service_id, SearchDataType::BeautyParlorService);
        }
        return $response;
    }

    /**
     * 修改美容院服务项目
     * @date: 2016年1月19日 
     * @author: chenxiaolin
     */
    public function updateAction()
    {
        $req = $this->request;
        if(!$req->isPost()) {
            $id = intval($req->getQuery('id', null, 0));
            if ($id < 1) {
                $this->view->disable();
                echo '参数异常！';
                return;
            }
            // 获取美容院信息
            $info = BeautyParlorService::findFirst('service_id = ' . $id);
            if (!$info) {
                $this->view->disable();
                echo '美容院不存在！';
                return;
            }
            $this->view->setVar('info', $info);
            $this->view->setVar('id', $id);
        } else {
            return $this->updateSave();
        }
    }

    /**
     * 保存修改后的服务项目信息
     * @date: 2016年1月19日 
     * @author: chenxiaolin
     */
    private function updateSave()
    {
        $req = $this->request;
        $response = new ResponseResult();
        $response->callback = $req->getPost('callback', null, 'parent.setFormResult');
        $response->callbackJavascriptTag = true;

        $id = intval($req->getPost('service_id'));
        if($id < 1) {
            $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '参数异常！');
            return $response;
        }

        $name = $req->getPost('service_name', null, '');
        $description = $req->getPost('service_description', null, '');
        $price = $req->getPost('service_price', null, '');
        $unit = $req->getPost('service_unit', null, '');
        $isSell = intval($req->getPost('service_is_sell', null, 0));
        $intro = $req->getPost('service_intro', null, '');

        // 判断空数据
        if(empty($name) || empty($description) || empty($price) || empty($intro)) {
            $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '参数异常！');
            return $response;
        }

        // 保存封面
        $uploadFile = new UploadPic();
        $uploadFile->request = $req;
        $cover = $uploadFile->beautyParlorCover();
        if($cover == false || !is_array($cover) || count($cover) == 0 || $cover[0] == false) {
            $serviceCover = null;
        } else {
            $serviceCover = $cover[0];
        }

        // 保存基础数据
        $service = BeautyParlorService::findFirst('service_id = ' . $id);
        $data = [];
        if($service->service_name != $name) {
            $data['service_name'] = $name;
        }
        if(!!$serviceCover) {
            $data['service_cover'] = $serviceCover;
        }
        if($service->service_description != $description) {
            $data['service_description'] = $description;
        }
        if($service->service_intro != $intro) {
            $data['service_intro'] = $intro;
        }
        if($service->service_price != $price) {
            $data['service_price'] = $price;
        }
        if($service->service_unit != $unit) {
            $data['service_unit'] = $unit;
        }
        if($service->service_is_sell != $isSell) {
            $data['service_is_sell'] = $isSell;
        }
        $service->update($data);

        SearchASync::Instance()->noticeSync($id, SearchDataType::BeautyParlorService);
        $response->sendResult('ok');
        return $response;
    }

    /**
     * 设置美容院状态
     * @return $this|ResponseResult
     */
    public function setStateAction()
    {
        $req = $this->request;
        if($req->isPost()) {
            $id = intval($req->getPost('id', null, 0));
            $state = intval($req->getPost('state', null, -10000));
            if(!in_array($state, [-1, 0, 1])) {
                return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
            }
            $info = BeautyParlorService::findFirst('service_id=' . $id);
            //服务项目存在：修改
            if($info) {
                $info->update(['service_state' => $state]);
            }
            SearchASync::Instance()->noticeSync($id, SearchDataType::BeautyParlorService);
            return (new ResponseResult())->sendResult('ok');
        } else {
            return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
        }
    }
}